<template>
  <div>
    <el-dialog
      :title="title"
      @close="cancel"
      :visible.sync="dialogVisible"
      width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="平台" prop="plaformName">
              <el-input v-model="form.plaformName" placeholder="请输入平台"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="域名" prop="domainName">
              <el-input v-model="form.domainName" placeholder="请输入域名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-input v-show="false" v-model="form.pic"></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getCityList} from "@/api/option";
import {isVehicleNumber} from "@/utils/utils";
import {addGPS, addPlatform, updateCar, updatePlatform, uploadVehicleAttachs} from "@/api/merchant/car";
import {listVehsupplierunit} from "@/api/supplerunit/vehsupplierunit";

export default {
  name: "addVehicle",
  created() {
    this.init()
  },
  data() {
    return {
      title: '新增门店',
      deviceOptions: [],
      previewDialogVisibleceshi: false,
      attachFilesceshi: [],
      previewImageListceshi: [],
      dialogVisible: false,
      shop_status: [],
      shop_ranges: [],
      citydata: [],
      rules: {
        plaformName: [
          {required: true, message: "平台名称不能为空", trigger: "blur"}
        ],
        domainName: [
          {required: true, message: "域名不能为空", trigger: "blur"}
        ],
        userName: [
          {required: true, message: "gps用户名不能为空", trigger: "blur"}
        ],
        gpsPassword: [
          {required: true, message: "gps密码不能为空", trigger: "blur"}
        ]
      },
      supplierUnitList: [],
      isCheck: '1',
      form: {
        vehicleDisting: '',
        supplierUnitId: '',
        plaformName: null,
        frameNumber: null,
        id: null,
        uniacid: null,
        sid: null,
        domainName: null,
        carnum: null,
        colour: null,
        gpsPassword: null,
        userName: null,
        userId: null,
        displacement: null,
        num: null,
        pic: null,
        content: null,
        moneytype: null,
        money: null,
        cartype: null,
        fee: null,
        serviceFee: null,
        zxServiceFee: null,
        rec: null,
        hot: null,
        createtime: null,
        status: null,
        actMoney: null,
        isDel: null,
        subscribeDuration: null,
        openXz: null,
        depositway: null,
        deposit: null,
        distributionOpen: null,
        distributionCommissiontype: null,
        firstmoney: null,
        secondmoney: null,
        thirdmoney: null,
        shopIds: null,
        vehpics: '',
        violationDeposit: null,
        sendServiceFee: null,
        pickServiceFee: null
      }
    }
  },
  methods: {
    //上传图片错误方法,公用
    handleExceedceshi(files, fileList) {
      this.$message.warning(`当前限制选择 ${this.limit} 个文件，本次选择了 ${files.length} 个文件，已自动过滤多余的文件。`);
    },
    init() {

      // 获取省市区三级
      getCityList({pid: 0}).then(response => {
        this.citydata = response.data;
      });
      listVehsupplierunit({pageNum: 1, pageSize: 100}).then(response => {
        let info = response.rows
        this.supplierUnitList = []
        for (let i = 0; i < info.length; i++) {
          if (info[i].unitType == 0) {
            this.supplierUnitList.push({label: info[i].personName, value: info[i].id})
          } else {
            this.supplierUnitList.push({label: info[i].supplierUnitName, value: info[i].id})
          }
        }
      });
    },
    submitForm() {
      // 获取图片及门店信息的值
      console.log('打印新增车辆信息', this.form)
      this.form.userId = this.$store.state.user.id
      if (this.form.id) {
        updatePlatform(this.form).then(res => {
          this.$message({
            message: '平台账号修改成功',
            type: 'success'
          });
          this.dialogVisible = false
        })
        this.$emit('update')
      } else {
        addPlatform(this.form).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          });
          this.dialogVisible = false
          this.$emit('update')
          window.parent.location.reload()
        })
      }
    },
    // 校验
    validateCarNumber(rule, value, callback) {
      if (isVehicleNumber(value)) {
        callback();
      } else {
        return callback(new Error("车牌号不正确"));
      }
    },
    validateAmount(rule, value, callback) {
      // 自定义校验函数的实现
      if (isNaN(value) || value < 0) {
        callback(new Error('金额必须是数字'))
      } else {
        callback()
      }
    },
    validateActAmount(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('限时活动价格必须是数字'))
      } else {
        callback()
      }
    },
    validateDepoAmount(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('车辆押金必须是数字'))
      } else {
        callback()
      }
    },
    validateFeeAmount(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('手续费必须是数字'))
      } else {
        callback()
      }
    },
    valServiceFeeAmount(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('基础服务费必须是数字'))
      } else {
        callback()
      }
    },
    valZxServiceFeeAmount(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('尊享服务费必须是数字'))
      } else {
        callback()
      }
    },
    valViolationAmount(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('违章押金必须是数字'))
      } else {
        callback()
      }
    },
    valSendServiceFee(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('送车上门服务费必须是数字'))
      } else {
        callback()
      }
    },
    valPickServiceFee(rule, value, callback) {
      if (isNaN(value) || value < 0) {
        callback(new Error('上门取车服务费必须是数字'))
      } else {
        callback()
      }
    },
    open(info) {
      if (info) {
        this.form = info
        console.log('form', this.form)

        // 将权限字段赋值给对应数据
        this.title = '修改平台'
      } else {
        this.title = '新增平台'
        this.form = {
          areaCity: '',
          businessHours: '',
          shopTel: '',
          linkman: '',
          ranges: '',
          status: '',
          serviceTel: ''
        }
      }
      this.dialogVisible = true
    },
    cancel() {
      this.dialogVisible = false
    }
  }
}
</script>
<style scoped lang="scss">

</style>
